package com.itheima.demo02;

import java.util.*;
import java.util.function.BiConsumer;

public class Test02 {
    public static void main(String[] args) {
        //有80名学生，投票A、B、C、D四个景点 ，统计出投票最多的景点
        //1.创建景点集合
        String[] arr = {"A", "B", "C", "D"};
        //2.创建学生投票记录
        ArrayList<String> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 80; i++) {
            int index = random.nextInt(4);
            list.add(arr[index]);
        }
        //3.创建HasMap对象记录景点次数
        HashMap<String, Integer> m = new HashMap<>();
        for (String name : list) {
            if (m.containsKey(name)) {
                //存在
                //先获取存在的景点次数
                Integer count = m.get(name);
                count++;
                m.put(name, count);
            } else {
                //不存在
                m.put(name, 1);
            }
        }
        //4.最最大值
        final Integer[] max1 = {0};
        m.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                if (integer > max1[0]){
                    max1[0] = integer;
                }
            }
        });
        System.out.println(max1[0]);

        m.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                if (Objects.equals(max1[0], integer)){
                    System.out.println(s);
                }
            }
        });



    }
}
